package com.xz.realdemo.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author 张志航     
 * @version V1.0.0
 * @projectName design_mode
 * @title     JDBCConn   
 * @package    com.xz.realdemo  
 * @date   2019/12/3 16:14  
 * @explain
 */
public class JDBCConn {

    private String ip;
    private String port;
    private static int poolSize = 3;

    private JDBCConn(String ip,String port){
        this.ip = ip;
        this.port = port;
    }

    // 静态内部类的方式保证单例
    private static class HolderClass{
        private static final List<JDBCConn> pool = new ArrayList<>();

        static {
            for (int i = 0; i < poolSize; i++) {
                pool.add(new JDBCConn("127.0.0."+i,"330"+i));
            }
        }
    }

    public static JDBCConn getConnnection(){
        List<JDBCConn> pool = HolderClass.pool;
        int i = new Random().nextInt(pool.size());
        return pool.get(i);
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getPort() {
        return port;
    }

    public void setPort(String port) {
        this.port = port;
    }
}
